/**
 * 设置全局的分页
 */
var page_ = new page({search:search});
/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid:'#datagrid',
	datagridtoolbar:'#datagrid-toolbar', 
	singleSelect:true});

function searchWservice(){
	search(page_);
}

/**
 * 查询
 * @param page
 */
function search(pg){
	var url = ctx + "/wservice/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}


/**
 * 获取查询条件
 * @returns {condition}
 */
function getCondition(){
	var condition = {organCode:$('#organCode').val()};
	return condition;
}


$(document).ready(function(){
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_接口管理_列表,
			callback:action,
			con:"#datagrid-toolbar"}
	findResourcesByUserId(authCondition);
	search(page_);
});

/**
 * 添加事件
 */
function add(){
	$('#dialog').dialog({
	    title: '添加',
	    width: 600,
	    height: 450,
	    href: ctx + "/web/system/wservice/add.jsp",
	    closed: false,
	    cache: false,
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    	findDrawerByParentCode(接口服务类型,setTypeValue);
	    },
	    modal: true,
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	addWservice();
            },
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 
 * 修改
 */
function edit(){
	var row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '修改',
	    width: 600,
	    height: 450,
	    href: ctx + "/web/system/wservice/add.jsp",
	    closed: false,
	    cache: false,
	    modal: true,
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',row);
	    	findDrawerByParentCode(接口服务类型,setTypeValue);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	update(row);
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 报送
 */
function report(){
	var row = getSelectedRow("#datagrid");
	var id = row.id;
	if(!row){
		return;
	}
	
	if(row.status=='已报送'){
		$.messager.show({title:'提示', msg:'该接口已报送',showType:'show'});
		return;
	}
	
	$("#listform #id").val(row.id);
	
	$.messager.confirm('提示', '确定要报送吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "Get",
	    		url : ctx + "/wservice/report",
	    		dataType : "json",
	    		data :{id:id},
	    		success : function(data) {
	    			if(!showMsg(data,"报送成功")){
        				return;
        			}
	    			search(page_);
	    		}
	    	});
	    }
	});
}
/**
 * 添加
 */
function addWservice(){
	var data = $("#addform").serializeJson();
	data.organId = $("#organId").val();
	$.ajax({
		type : "POST",
		url : ctx + "/wservice/create",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"添加成功")){
				return;
			}
			search(page_);
			$('#dialog').dialog('close');
		}
	});
}

/**
 * 修改
 */

function update(row){
	var data = $("#addform").serializeJson();
	$.ajax({
		type : "POST",
		url : ctx + "/wservice/update",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"修改成功")){
				return;
			}
			search(page_);
			$('#dialog').dialog('close');
		}
	});
}

/**
 * 设置接口类型下拉框
 * @param data
 */
function setTypeValue(data){
	var value = $('#addform #type').combobox('getValue');
	$('#addform #type').combobox({ 
		valueField:'code',
        textField:'value',
        data:data,
        panelHeight:'auto'
	});
	
	if($.trim(value).length != 0){
		$('#addform #type').combobox('select',value);
	}
}